import sqlite3
import urllib
import time

#CONNECT TO DATABASE.
cxn=sqlite3.connect('../../data/ted5000.sqlite')
cur=cxn.cursor()
cur.execute('select * from data order by dateTime')
cxn2=sqlite3.connect('../../data/ted5000_2.sqlite')
cur2=cxn2.cursor()
cur2.execute('CREATE TABLE data(dateTime  DATETIME ' + \
            'PRIMARY KEY, kW FLOAT, V FLOAT)')
cxn2.commit();

rows=cur.fetchall()
sqlText = 'begin;'
for row in rows:
    date, time = row[0].split()
    m, d, y = date.split('/')
    date = '-'.join([y,m,d])
    sqlText += ' INSERT INTO data VALUES(datetime("%s %s"), %s, %s); ' %\
                (date, time, row[1], row[2])
sqlText += ' commit;'
cur2.executescript(sqlText)

cxn2.commit()
cxn.close()
cxn2.close()
